/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.sharbool;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import alloy.ast.ASTDepthFirstVisitor;
import alloy.ast.LeafId;
import alloy.ast.Node;
import alloy.ast.VariableExpr;
import alloy.util.Dbg;

/**
 * Does some sanity checks on the results of template detection --
 * not currently used.
 */
public class TemplateCheckVisitor extends ASTDepthFirstVisitor {
    private Map _node2vars;
    private Map _node2templInst;

    public TemplateCheckVisitor(Map node2vars_, Map node2templInst_) {
	_node2vars = node2vars_;
	_node2templInst = node2templInst_;
    }
    
    public void visit(Node node_) {
	TemplateInstance templInst = (TemplateInstance)_node2templInst.get(node_);
	Set vars = (Set)_node2vars.get(node_);
	// for each quantified variable on which this node depends,
	// check that the variable appears in the parameter list of the template.
	for (Iterator varIter = vars.iterator(); varIter.hasNext();) {
	    LeafId leafId = (LeafId)varIter.next();
	    boolean found = false;
	    for (Iterator argIter = templInst.args.iterator(); !found && argIter.hasNext();) {
		VariableExpr arg = (VariableExpr)argIter.next();
		Dbg.chk(arg.isQuantified);
		if (arg.getLeafId().equals(leafId))
		    found = true;
	    }
	    Dbg.chk(found);
	}
	for (int i=0; i<node_.numChildren(); i++)
	    visit(node_.childAt(i));
    }
}
